package com.example.pingze.service;

import com.example.pingze.entity.Manager;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.ManagerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ManagerService {
    @Autowired
    private ManagerRepository repository;

    public Manager login(String no, String password) {
        Manager manager = repository.findManagerByNo(no);
        if(manager==null){
            throw new PingZeException("管理员账号不存在");
        }
        if(!manager.getPassword().equals(password)){
            throw new PingZeException("管理员账号或密码错误");
        }
        return manager;
    }
    public Manager register(String no,String password){
        Manager manager = repository.findManagerByNo(no);
        if(manager != null){
            throw new PingZeException("该账号已经存在");
        }
        Manager add = new Manager();
        add.setId(UUID.randomUUID().toString());
        add.setNo(no);
        add.setPassword(password);
        //男，普通管理员
        add.setSex(1);
        add.setRoleId(2);
        repository.save(add);
        return add;
    }
    public Manager add(String no,String password,Integer sex){
        Manager manager = repository.findManagerByNo(no);
        if(manager != null){
            throw new PingZeException("该账号已经存在");
        }
        Manager add = new Manager();
        add.setId(UUID.randomUUID().toString());
        add.setNo(no);
        add.setPassword(password);
        add.setSex(sex);
        add.setRoleId(2);
        repository.save(add);
        return add;
    }

    public Page getManagerList(Manager manager, Pageable pageable){
        Specification<Manager> specification=new Specification<Manager>() {
            @Override
            public Predicate toPredicate(Root<Manager> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if(!StringUtils.isEmpty(manager.getNo())){
                    predicates.add(criteriaBuilder.equal(root.get("no"),manager.getNo()));
                }
                if(manager.getSex()!=null){
                    predicates.add(criteriaBuilder.equal(root.get("sex"),manager.getSex()));
                }
                if(manager.getRoleId()!=null){
                    predicates.add(criteriaBuilder.equal(root.get("roleId"),manager.getRoleId()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page=repository.findAll(specification,pageable);
        return page;
    }

    public Manager edit(String id,String no,String password,Integer sex,Integer roleId){
        Manager edit=repository.findManagerById(id);
        if(!edit.getNo().equals(no)){
            edit.setNo(no);
        }
        if(!edit.getPassword().equals(password)){
            edit.setPassword(password);
        }
        if(!edit.getRoleId().equals(roleId)){
            edit.setRoleId(roleId);
        }
        if(!edit.getSex().equals(sex))
            edit.setSex(sex);
        repository.save(edit);
        return edit;
    }
}
